{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6c4b5b34",
   "metadata": {},
   "source": [
    "# 判断\n",
    "\n",
    "判断，基于一定的条件，决定是否要执行特定的一段代码，例如判断一个数是不是正数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "01a1309f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey!\n",
      "x is positive\n"
     ]
    }
   ],
   "source": [
    "x = 0.5\n",
    "if x > 0:\n",
    "    print(\"Hey!\")\n",
    "    print(\"x is positive\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b691e17",
   "metadata": {},
   "source": [
    "如果 x > 0 为 False ，那么程序将不会执行两条 print 语句。\n",
    "\n",
    "虽然都是用 if 关键词定义判断，但与C，Java等语言不同，Python不使用大括号将 if 语句控制的区域包含起来。Python使用的是缩进方法。同时，也不需要用小括号将判断条件括起来。\n",
    "\n",
    "上面例子中的这两条语句：\n",
    "```python\n",
    "    print(\"Hey!\")\n",
    "    print(\"x is positive\")\n",
    "```\n",
    "就叫做一个代码块，同一个代码块使用同样的缩进值，它们组成了这条 if 语句的主体。\n",
    "\n",
    "不同的缩进值表示不同的代码块，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d982f769",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey!\n",
      "x is positive\n",
      "This is still part of the block\n",
      "This isn't part of the block, and will always print.\n"
     ]
    }
   ],
   "source": [
    "x = 0.5\n",
    "if x > 0:\n",
    "    print(\"Hey!\")\n",
    "    print(\"x is positive\")\n",
    "    print(\"This is still part of the block\")\n",
    "print(\"This isn't part of the block, and will always print.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c27625dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This isn't part of the block, and will always print.\n"
     ]
    }
   ],
   "source": [
    "x = -0.5\n",
    "if x > 0:\n",
    "    print(\"Hey!\")\n",
    "    print(\"x is positive\")\n",
    "    print(\"This is still part of the block\")\n",
    "print(\"This isn't part of the block, and will always print.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b4cdc10",
   "metadata": {},
   "source": [
    "在这两个例子中，最后一句并不是if语句中的内容，所以不管条件满不满足，它都会被执行。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "837965e8",
   "metadata": {},
   "source": [
    "## 完整的 if 结构\n",
    "\n",
    "一个完整的 if 结构通常如下所示（注意：条件后的 : 是必须要的，缩进值需要一样）：\n",
    "\n",
    "```python\n",
    "if <condition 1>:\n",
    "    <statement 1>\n",
    "    <statement 2>\n",
    "elif <condition 2>: \n",
    "    <statements>\n",
    "else:\n",
    "    <statements>\n",
    "```\n",
    "\n",
    "当条件1被满足时，执行 if 下面的语句，当条件1不满足的时候，转到 elif ，看它的条件2满不满足，满足执行 elif 下面的语句，不满足则执行 else 下面的语句。\n",
    "\n",
    "对于上面的例子进行扩展："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "43d0a2cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is zero\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "if x > 0:\n",
    "    print(\"x is positive\")\n",
    "elif x == 0:\n",
    "    print(\"x is zero\")\n",
    "else:\n",
    "    print(\"x is negative\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68463c5d",
   "metadata": {},
   "source": [
    "elif 的个数没有限制，可以是1个或者多个，也可以没有。\n",
    "\n",
    "else 最多只有1个，也可以没有。\n",
    "\n",
    "可以使用 and ， or , not 等关键词结合多个判断条件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4accaedd",
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = 10, -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4678b8bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x > 0 and y < 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9471c4ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not x > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "857c8ed7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x < 0 or y < 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "178ff3f4",
   "metadata": {},
   "source": [
    "例如，判断一个年份是不是闰年，按照闰年的定义，这里只需要判断这个年份是不是能被4整除，但是不能被100整除，或者正好被400整除："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e1e3f9e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is not a leap year.\n"
     ]
    }
   ],
   "source": [
    "year = 1900\n",
    "if year % 400 == 0:\n",
    "    print(\"This is a leap year!\")\n",
    "# 两个条件都满足才执行\n",
    "elif year % 4 == 0 and year % 100 != 0:\n",
    "    print(\"This is a leap year!\")\n",
    "else:\n",
    "    print(\"This is not a leap year.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fa77354",
   "metadata": {},
   "source": [
    "## 值的测试\n",
    "\n",
    "Python不仅仅可以使用布尔型变量作为条件，它可以直接在if中使用任何表达式作为条件：\n",
    "\n",
    "大部分表达式的值都会被当作`True`，但以下表达式值会被当作`False`：\n",
    "\n",
    "- `False`\n",
    "- `None`\n",
    "- `0`，`0.0`\n",
    "- 空字符串，空列表，空字典，空集合"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "66f92e77",
   "metadata": {},
   "source": [
    "## if表达式\n",
    "\n",
    "关键词if还可以写到表达式中，构成一个if表达式：\n",
    "```\n",
    "<value1> if <condition> else <value2>\n",
    "```\n",
    "当条件`<condition>`满足时，表达式值为`<value1>`，否则为`<value2>`。\n",
    "\n",
    "例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a542bec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7afe0d09",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = \"big\" if a < 10 else \"small\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d2dda615",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'big'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb7a11dd",
   "metadata": {},
   "source": [
    "## and/or的返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32454c93",
   "metadata": {},
   "source": [
    "`and/or`运算的返回值\n",
    "- `and`返回值：第一个为假的值，或者最后一个值\n",
    "- `or`返回值：第一个为真的值，或者最后一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ff6e152a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 and 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6c8a9f0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 and [] and 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "74f21535",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 or 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8830f097",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0 or []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "080240e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
